import Vue from 'vue'
import VueRouter from 'vue-router'
import NotFound from '../components/404.vue'
import Login from '../components/Login.vue' //引入
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Student from '../components/UserList/Studentt/student.vue'
import Space from '../components/ContactList/space/space.vue'
import Adds from'../components/ContactList/space/Adds.vue'
import GroupIndex from '../components/Group/GroupIndex.vue'
import CreateGroup from '../components/Group/CreateGroup.vue'
import GoodsList from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Shop from '../components/Shop/shop.vue'
import ShopList from '../components/Shop/shop.vue'
import GroupDetails from '../components/Group/GroupDetails.vue'
import Register from '../components/Register.vue'
import Square from '../components/Square/square.vue'
import Addsq from '../components/Square/Addsq.vue'

Vue.use(VueRouter)

const routes = [{
		path: "/",
		redirect: "/login"
	},
	{
		path: "/login",
		component: Login
	},
	{
		path: "/register",
		component: Register
	},
	{
		path: "/home",
		component: Home,
		redirect:"/welcome",
		children:[
			{path:"/welcome",component:Welcome},
			{path:"/student",component:Student},
			{path:"/space",component:Space},
            {path:"/space/Adds",component:Adds},
			{path:"/group",component:GroupIndex},
			{path:"/market",component:GoodsList},
			{path:"/goods/add",component:Add},
			{path:"/shop",component:ShopList},
			{path:"/shop",component:Shop},
	        {path:"/square",component:Square},
	        {path:"/square/Addsq",component:Addsq},
		]
	},
	{
	    path:"/Group/CreateGroup",
		
		        
		component:CreateGroup,
	},
	{
	    path:"/Group/GroupDetails",
		component:GroupDetails,
	},
	{
		path:'*',
		component:NotFound
	}
]

const router = new VueRouter({
	mode:'history',//url模式:history为不带#模式
	routes
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


//路由导航守卫：保护路由访问的方法
router.beforeEach((to, from, next) => {
	//to 想要去
	//from 从哪访问
	//next:之后要做的任务，是一个函数  next（）放行， next（'/URL'）强制跳转的路径。
	
	if (to.path === '/login' || to.path === '/register') return next();
	
	//从session中调取token
	//并进行是否有值的判断
	//有值=>向下跳转
	//无值=>路由向登录页面跳转
	const tokenStr = window.sessionStorage.getItem("token");
	if(!tokenStr) return next('/login');
	next();
})

export default router